<?php

namespace App\Http\Controllers\Admin\Project;

use App\Exceptions\LogicException;
use App\Http\Controllers\Controller;
use App\Models\Adminer\Admins;
use App\Models\Projects\Product;
use App\Models\Projects\Project;
use App\Utils\QueryUtil;
use App\Utils\ResUtil;

class ProductController extends Controller
{

    public function list()
    {
        $request = request()->all();
        $query = Product::where(function($query) use($request) {
            QueryUtil::selectBuild($query, $request, [
                ['name', 'like', 'name'],
                ['enable', '='],
                ['tags', 'like', 'tags'],
                ['version', 'like', 'version'],
                ['remark', 'like', 'remark'],
                ['note', 'like', 'note'],
                ['created_adminer', 'like', 'created_adminer'],
                ['updated_adminer', 'like', 'updated_adminer'],
                ['cate_id', '='],
                ['project_id', '='],
                ['id', '='],
            ]);
        });

        $total = $query->count();
        $models = QueryUtil::queryList($query, 'sort');
        $list = [];
        foreach($models as $i => $v) {
            $list[$i] = $v?->label()?->SignOssUrlToExplodeImg()?->Explode()?->toArray();
        }
        $hasMore = QueryUtil::hasMore(sizeof($list));
        return $this->ok(compact('list', 'total', 'hasMore'));
    }

    public function edit()
    {
        $data = request()->all();
        $rule = [
            'id' => '',
            'project_id' => '',
            'version' => 'string|required',
            'name' => 'string|required',
            'icon' => 'string|required',
            'tags' => 'array|required',
            'sort' => '',
            'remark' => '',
            'note' => '',
            'enable' => '',
            //'created_adminer' => '',
            //'updated_adminer' => '',
        ];
        $validator = validatorService()->checkValidator($rule, $data, Product::$fillables);
        if ($validator['code'] != 200) {
            throw new LogicException($validator['data']);
        }

        if($data['id'] ?? '') {
            $product = Product::find($data['id']);
            if(!$product) {
                throw new LogicException(ResUtil::NOT_DATA);
            }
        } else {
            $product = new Product();
            $product->enable = 1;
            $product->sort = 1;
            $product->created_adminer = Admins::getInfoById($this->authAdmin()->id);
        }

        $tags = implode(',', $data['tags']);

        $project = Project::find($data['project_id']);
        if(!$project) {
            throw new LogicException('项目不存在');
        }

        $product->fill([
            'name' => $data['name'] ?? '',
            'tags' => $tags,
            'version' => $data['version'],
            'icon' => $data['icon'] ?? '',
            'sort' => $data['sort'] ?? 1,
            'remark' => $data['remark'] ?? '',
            'enable' => $data['enable'] ?? 1,
            'cate_id' => $project->cate_id,
            'project_id' => $project->id,
            'note' => $data['note'] ?? '',
            'updated_adminer' => Admins::getInfoById($this->authAdmin()->id),
        ]);
        $product->save();
        return $this->ok();
    }

    public function enable()
    {
        $data = request()->all();
        $rule = [
            'id' => 'integer|required',
            'enable' => 'integer|required|in:0,1',
        ];
        $validator = validatorService()->checkValidator($rule, $data, Product::$fillables);
        if ($validator['code'] != 200) {
            throw new LogicException($validator['data']);
        }
        $cate = Product::find($data['id']);
        if(!$cate) {
            throw new LogicException(ResUtil::NOT_DATA);
        }
        $cate->enable = $data['enable'];
        $cate->save();
        return $this->ok();
    }

    public function allProduct()
    {
        $list = Product::select('id', 'name')->get();
        return $this->ok(compact('list'));
    }


}
